package com.jswlw.controller.admin.base;

import com.jswlw.controller.BaseController;
import com.jswlw.entity.base.SysConfig;
import com.jswlw.service.base.SysConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin/sys_config/")
public class AdminSysConfigController extends BaseController {

    @Resource
    SysConfigService sysConfigService;

    /**
     * 主页
     *
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(Model model) {
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("info",sysConfigService.findOne(1));
        modelAndView.setViewName("sys_config/index");
        return modelAndView;
    }

    /**
     * 保存
     *
     * @param model
     * @param goods
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(Model model, SysConfig sysConfig) {
        try {
            if (sysConfig.getId() == null) {
                sysConfigService.insert(sysConfig);
            } else {
                sysConfigService.update(sysConfig);
            }
            return this.successResult(null);
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("保存失败" + e.getMessage());
        }
    }
}
